package com.bl.student.api.controller.teacher;

import com.bl.student.api.controller.BaseController;
import com.bl.student.common.ApiResponseBody;
import com.bl.student.dao.redis.model.TeacherInfo;
import com.bl.student.service.biz.teacher.TeacherMainService;
import com.bl.student.service.biz.teacher.TeacherService;
import com.bl.student.service.vo.main.LoginResponseVo;
import com.bl.student.service.vo.teacher.TeacherInfoModifyVo;
import com.bl.student.service.vo.teacher.TeacherLoginResponseVo;
import com.bl.student.service.vo.teacher.TeacherLoginVo;
import com.bl.student.service.vo.teacher.TeacherRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created by yujingyi on 2018/2/7.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/student/apis/partya"})
@Api(value = "甲方注册登陆相关api", description = "甲方注册登陆相关api")
public class TeacherMainController extends BaseController {


    static final private String OPEN_ID_COOKIE_NAME = "OPEN_ID";
    static final private String MOBILE_COOKIE_NAME = "MOBILE";
    static final private String UNION_ID_COOKIE_NAME = "UNION_ID";
    static final private String USER_ID_COOKIE_NAME = "USER_ID";

    @Autowired
    TeacherMainService teacherMainService;

    @Autowired
    TeacherService teacherService;

//    @ApiOperation(value = "注册接口", notes = "")
//    @PostMapping(value = {"/register"})
//    @ResponseBody
//    public ApiResponseBody register(
//            @Valid @RequestBody TeacherRegisterVo body, BindingResult result,
//            HttpServletRequest request) {
//
//        ApiResponseBody validResult = validParams(result);
//        if (validResult != null) {
//            return validResult;
//        }
//
//        LoginResponseVo loginResponseVo = teacherMainService.register(body, getOpenId(request));
//
//        return ApiResponseBody.createSuccessBody(null);
//    }

    @ApiOperation(value = "登陆", notes = "登陆")
    @PostMapping(value = {"/login"})
    public ApiResponseBody<TeacherLoginResponseVo> login(
            @Valid @RequestBody TeacherLoginVo body, BindingResult result,
            HttpServletRequest request, HttpServletResponse response) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        TeacherLoginResponseVo teacherLoginResponseVo = teacherMainService.appLogin(body);

        Cookie tokenCookie = new Cookie(MOBILE_COOKIE_NAME, teacherLoginResponseVo.getMobile());
        tokenCookie.setPath("/");
        tokenCookie.setMaxAge(3600 * 24 * 30);
        Cookie openIdCookie = new Cookie(OPEN_ID_COOKIE_NAME, teacherLoginResponseVo.getOpenId());
        openIdCookie.setPath("/");
        Cookie userIdCookie = new Cookie(USER_ID_COOKIE_NAME, teacherLoginResponseVo.getId());
        userIdCookie.setPath("/");

        response.addCookie(tokenCookie);
        response.addCookie(userIdCookie);
        response.addCookie(openIdCookie);

        return ApiResponseBody.createSuccessBody(teacherLoginResponseVo);
    }

    @ApiOperation(value = "登出", notes = "")
    @ApiImplicitParams({
    })
    @GetMapping(value = {"/logout"})
    public ApiResponseBody logout(
            HttpServletRequest request, HttpServletResponse response) {
//        teacherMainService.logout((TeacherInfo) getNormalUserInfo(request));
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "老师信息修改接口", notes = "")
    @ApiImplicitParams({
    })
    @PostMapping(value = {"/modify"})
    @ResponseBody
    public ApiResponseBody<TeacherInfoModifyVo> modify(
            @Valid @RequestBody TeacherInfoModifyVo body, BindingResult result,
            HttpServletRequest request) {

        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }

        teacherMainService.modify(body, (TeacherInfo) getNormalUserInfo(request));

        return ApiResponseBody.createSuccessBody(null);
    }


}
